package com.ums.scumspay.controller;

import cn.hutool.json.JSONUtil;
import com.ums.scumspay.common.Resp;
import com.ums.scumspay.entity.PayOrder;
import com.ums.scumspay.entity.PayQueryReq;
import com.ums.scumspay.entity.req.PayCrtReq;
import com.ums.scumspay.entity.req.RefundReq;
import com.ums.scumspay.entity.resp.PayResp;
import com.ums.scumspay.exception.BizCode;
import com.ums.scumspay.exception.BizException;
import com.ums.scumspay.service.PayCreateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/crt")
public class PayCreateController {

    @Autowired
    private PayCreateService payCreateService;

    @PostMapping("/b2c_pay")
    public Resp<PayResp> b2cPay(@RequestBody PayCrtReq payCrtReq){
        if ((payCrtReq.getPhone() == null) || !Pattern.matches("^1[3-9]\\d{9}$", payCrtReq.getPhone())) {
            return Resp.error("手机号码为空或者格式错误");
        }
        return Resp.success(payCreateService.b2cPay(payCrtReq));
    }

    @PostMapping("/c2b_pay")
    public Resp<PayResp> c2bPay(@RequestBody PayCrtReq payCrtReq){
        if ((payCrtReq.getPhone() == null) || !Pattern.matches("^1[3-9]\\d{9}$", payCrtReq.getPhone())) {
            return Resp.error("手机号码为空或者格式错误");
        }
        return Resp.success(payCreateService.c2bPay(payCrtReq));
    }


    @PostMapping("/h5_pay")
    public Resp<String> h5Pay(@RequestBody PayCrtReq payCrtReq){
        if ((payCrtReq.getPhone() == null) || !Pattern.matches("^1[3-9]\\d{9}$", payCrtReq.getPhone())) {
            return Resp.error("手机号码为空或者格式错误");
        }
        return Resp.success(payCreateService.h5Pay(payCrtReq));
    }

    @PostMapping("/b2c_query")
    public Resp<String> b2cQuery(@RequestBody PayQueryReq req){
        return Resp.success(payCreateService.b2cQuery(req));
    }

    @PostMapping("/c2b_query")
    public Resp<String> c2bQuery(@RequestBody PayQueryReq req){
        return Resp.success(payCreateService.c2bQuery(req));
    }

    @PostMapping("/h5_query")
    public Resp<String> h5Query(@RequestBody PayQueryReq req){
        return Resp.success(payCreateService.h5Query(req));
    }


    @PostMapping("/b2c_refund")
    public Resp<Boolean> b2cRefund(@RequestBody RefundReq req){
        return Resp.success(payCreateService.b2cRefund(req));
    }

    @PostMapping("/c2b_refund")
    public Resp<Boolean> c2bRefund(@RequestBody RefundReq req){
        return Resp.success(payCreateService.c2bRefund(req));
    }

    @PostMapping("/h5_refund")
    public Resp<Boolean>h5Refund(@RequestBody RefundReq req){
        return Resp.success(payCreateService.h5Refund(req));
    }

    public static void main(String[] args) {
        System.out.println(JSONUtil.toJsonStr(Resp.success("Ssss")));
    }


    @PostMapping("/urlNotify")
    public void urlNotify(@RequestParam String extOrderNo){
        payCreateService.urlNotify(extOrderNo);
    }


    @PostMapping("/query")
    public Resp<String> queryPayStatus(@RequestBody PayResp payResp){
        return Resp.success(payCreateService.querPayStatus(payResp.getExtOrderNo()));
    }

    @GetMapping("/test")
    public Resp<PayResp> test(){
        PayCrtReq payCrtReq = new PayCrtReq();
        payCrtReq.setPhone("12312");
        if ((payCrtReq.getPhone() == null) || !Pattern.matches("^1[3-9]\\d{9}$", payCrtReq.getPhone())) {
            // throw new BizException(BizCode.ERROR,"手机号码为空或者格式错误");
            return Resp.error("手机号码为空或者格式错误");
        }
        System.out.println(payCrtReq.toString());
        return null;
    }
}
